package com.fht.service.inventory.impl;

import com.fht.entity.PurchaseOrdersParticulars;
import com.fht.mapper.inventory.PurchaseOrdersParticularsMapper;
import com.fht.service.inventory.PurchaseOrdersParticularsService;
import com.fht.util.UnitConversionUtil;
import com.fht.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Service
public class PurchaseOrdersParticularsServiceImpl implements PurchaseOrdersParticularsService {

    @Resource
    public PurchaseOrdersParticularsMapper purchaseOrdersParticularsMapper;

    @Override
    public LayUITableVO<List<PurchaseOrdersParticulars>> findPageById(String id) {


        List<PurchaseOrdersParticulars> list = purchaseOrdersParticularsMapper.findPageById(id);

        for (PurchaseOrdersParticulars purchaseOrdersParticulars : list) {

            purchaseOrdersParticulars.setConversionName(purchaseOrdersParticulars.getConversion()+purchaseOrdersParticulars.getRepertoryUnit()+"/"+purchaseOrdersParticulars.getPurchaseUnit());
            purchaseOrdersParticulars.setUnitPrice(UnitConversionUtil.conversion(Integer.valueOf(purchaseOrdersParticulars.getUnitPrice())));
            purchaseOrdersParticulars.setSubtotalString(UnitConversionUtil.conversion(purchaseOrdersParticulars.getSubtotal()));

        }

        LayUITableVO<List<PurchaseOrdersParticulars>> vo = new LayUITableVO<>();
        vo.setData(list);
        return vo;
    }

    @Override
    @Transactional
    public void save(List<PurchaseOrdersParticulars> dataList) {


        for (PurchaseOrdersParticulars purchaseOrdersParticulars : dataList) {
            purchaseOrdersParticulars.setSubtotal(UnitConversionUtil.conversion(purchaseOrdersParticulars.getSubtotalString()));

            Example example = new Example(PurchaseOrdersParticulars.class);
            Example.Criteria c = example.createCriteria();
            c.andEqualTo("id",purchaseOrdersParticulars.getId());
            c.andEqualTo("materialId",purchaseOrdersParticulars.getMaterialId());
            List<PurchaseOrdersParticulars> purchaseOrdersParticularsList = purchaseOrdersParticularsMapper.selectByExample(example);
            if (purchaseOrdersParticularsList.size()>0){
                Example example1 = new Example(PurchaseOrdersParticulars.class);
                Example.Criteria c1 = example1.createCriteria();
                c1.andEqualTo("id",purchaseOrdersParticulars.getId());
                c1.andEqualTo("materialId",purchaseOrdersParticulars.getMaterialId());
                purchaseOrdersParticularsMapper.updateByExampleSelective(purchaseOrdersParticulars,example1);
            }else {
                purchaseOrdersParticularsMapper.insertSelective(purchaseOrdersParticulars);
            }


        }


    }

    @Override
    @Transactional
    public void deleteById(String id) {
        Example example = new Example(PurchaseOrdersParticulars.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("id",id);
        purchaseOrdersParticularsMapper.deleteByExample(example);
    }

    @Override
    public void deleteByIdAndMaterialId(String id, String materialId) {
        Example example = new Example(PurchaseOrdersParticulars.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("id",id);
        c.andEqualTo("materialId",materialId);
        purchaseOrdersParticularsMapper.deleteByExample(example);
    }
}
